package com.rjkj.medicalwaste.adapter

import android.content.Context
import android.text.SpannableString
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.style.RelativeSizeSpan
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import com.rjkj.medicalwaste.bean.GarbageBean
import com.rjkj.medicalwaste.bean.MedicalWasteType
import com.rjkj.medicalwaste.databinding.ItemCollectListBinding

class CollectList2Adapter(var list: List<GarbageBean>, val context: Context, private val itemClick: ItemClick<GarbageBean>) : RecyclerView.Adapter<CollectList2Adapter.ViewHolder>() {


    class ViewHolder(private val binding: ItemCollectListBinding) : RecyclerView.ViewHolder(binding.root) {

        fun bind(garbageBean: GarbageBean, context: Context) {
            binding.garbage = garbageBean
            garbageBean.garbageTime.split(" ").let {
                if (it.size >= 2) {
                    binding.tvTimeYmd.text = it[0].substring(2, it[0].length)
                    binding.tvTimeHms.text = it[1]
                }
            }
            binding.tvWeight.text = garbageBean.garbageWeight

            MedicalWasteType.getTypeByTypeName(garbageBean.garbageType).let {
                binding.tvType.text = it.alias
                if (it.bgId != 0)
                    binding.tvType.setBackgroundResource(it.bgId)
            }

        }
    }


    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        list[position].let { garbage->
            holder.bind(garbage, context)
            holder.itemView.setOnClickListener{
                itemClick.onClicked(it,garbage)
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = ItemCollectListBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false)
        return ViewHolder(binding)
    }

    override fun getItemCount(): Int {
        return list.size
    }
}